using System.Collections;
using System.Collections.Generic;
using UnityEngine;

[RequireComponent(typeof(ActionDistance))]
public class Vedro : MonoBehaviour
{

    private Animator anim;
    private AudioSource audioVedro;
    //private bool s;
    private bool isOpen = false;
    private bool enabledVedro = true;
    private ActionDistance action;

    void Start()
    {
        anim = gameObject.GetComponent<Animator>();
        audioVedro = gameObject.GetComponent<AudioSource>();
        action = gameObject.GetComponent<ActionDistance>();
        action.s = false;
    }


    private void OnMouseDown()
    {
        if (action.s && enabledVedro)
        {
            StartCoroutine(open());
        }
    }

    //private void OnTriggerEnter2D(Collider2D collision)
    //{
    //    if (collision.CompareTag("Player"))
    //        s = true;
    //}

    //private void OnTriggerExit2D(Collider2D collision)
    //{
    //    if (collision.CompareTag("Player"))
    //        s = false;
    //}

    private IEnumerator open()
    {
        enabledVedro = false;
        isOpen = !isOpen;
        anim.SetBool("isOpen", isOpen);
        audioVedro.Play();
        yield return new WaitForSeconds(1.2f);
        enabledVedro = true;
    }
}
